package irri.soils.ssnm;

import android.content.Intent;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.TextView;


public class ActivityN extends SSNM{
	private Bundle b;
	private NoImeEditText cav;
	private NoImeEditText cav2;
	private int bound;
	private int bound_1;
	private View linearLayout3_1;
	private TextView question;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.n);
        setCustomTitle();
        TextView tv = (TextView) findViewById(R.id.question);
        b = this.getIntent().getExtras();
        question = (TextView) findViewById(R.id.question);
        question.setText(get("ya0"));
        double ha = b.getDouble("ha");
        tv.setText(""+tv.getText()+" "+df.format(ha)+" "+get("ya1")+" ");
        
        int i = b.getInt("season");
        int crop = b.getInt("crop");
        String sea = "";
        if(crop<3) sea = i==1 ? get("season1") : 
        	get("season3");
        else sea = (i==1 ? (get("early")+" "+get("wet")) : 
        	(i==2 ? (get("late")+" "+get("wet")) : get("dry"))) + 
        	(SSNM.d==0 ? " season" : "");
        String var = "";
        if(b.getInt("CE")==1){
        var = b.getString("varName");
        if(var.equals(get("variety22"))) var = b.getString("otherVar");
        }else var = "selected";
        tv.setText(Html.fromHtml(tv.getText()+"<font color=\"#259217\">"+sea+
        		"</font> "+" "+get("ya2")+" "+var+" "+
        		get("ya3")));
        
        Double ya = 9.75;
        Double MC = !sea.equals(get("dry")+(SSNM.d==0 ? " season" : "")) ? 25.0 : 22.0;
        Double FW = ya/((100-MC)/86);
        int kg = b.getInt("kg");
        
        Double boundDouble = FW/(1.0*kg/1000/ha);
        bound = (int) Math.floor(boundDouble);
        System.out.println(bound+"-----");
        TextView tv_1 = (TextView) findViewById(R.id.question_1);
    	tv_1.setText(get("yp0")+" "+df.format(ha)+" "+get("yp1")+" ");
    	if(crop<3) sea = i==1 ? get("season3") : 
        	get("season1");
        else sea = (i==1 ? get("dry") : (i==2 ? get("early")+" "+get("wet") : 
        	(get("late")+" "+get("wet"))))+(SSNM.d==0 ? " season" : "");
        tv_1.setText(Html.fromHtml(tv_1.getText() +
        		"<font color=\"#259217\">"+sea+"</font>"+"."));

        ya = 9.75;
        MC = sea.equals(get("dry")+(SSNM.d==0 ? " season" : "")) ? 
        		22.0 : 25.0;
        FW = ya/((100-MC)/86);
        kg = b.getInt("kg");
        
        boundDouble = FW/(1.0*kg/1000/ha);
        bound_1 = (int) Math.floor(boundDouble);
        bound_1 = b.getInt("crop")==1 ? bound : bound_1;
        linearLayout3_1 = findViewById(R.id.linearLayout3_1);
        
        
        cav = (NoImeEditText) findViewById(R.id.cav);
        cav.toHide(linearLayout3_1);
        cav2 = (NoImeEditText) findViewById(R.id.cav2);
        
        if(b.containsKey("cav")){
        	cav.setText(""+b.getInt("cav"));
        	if(b.getInt("crop")>1) cav2.setText(""+b.getInt("cav2"));
        }
        
        
        TextView prompt1 = (TextView) findViewById(R.id.textView1);
        prompt1.setText(get("yprompt")+" "+bound+".");
        
        
        TextView prompt2 = (TextView) findViewById(R.id.TextView01);
        prompt2.setText(get("yprompt")+" "+bound_1+".");
        
        mInputView = (KeyboardView) findViewById(R.id.keyboard);
        
        mInputView.setOnKeyboardActionListener(this);
        mSymbolsKeyboard = new LatinKeyboard(this, R.xml.number);
        mInputView.setKeyboard(mSymbolsKeyboard);
    }
    
	@Override
	public void onBackPressed() {
		Intent intent = new Intent(ActivityN.this,
				ActivityM.class);
		intent.putExtras(b);
		startActivity(intent);
		finish();
	}
	
	/* (non-Javadoc)
	 * @see irri.soils.ssnm.SSNMActivity#onKey(int, int[])
	 */
	@Override
	public void onKey(int primaryCode, int[] keyCodes) {
		View v = this.getCurrentFocus();
		try{
			TextView tv = (TextView) v;
			String str = ""+tv.getText();
			
			if (primaryCode == Keyboard.KEYCODE_DELETE) {
				onDelete(tv, str);
			}
			else if(primaryCode == 10){
				//try{
					if(tv.getId()==cav.getId()&&b.getInt("crop")==1||
						tv.getId()==cav2.getId()){
					Intent intent = new Intent(ActivityN.this, ActivityO.class);
					try{
						int cavVal = Integer.parseInt(""+cav.getText());
						int cav2Val = b.getInt("crop")==1 ? 
								cavVal : Integer.parseInt(""+cav2.getText());
						if(cavVal<=bound&&cav2Val<=bound_1){
							b.putInt("cav", cavVal);
							b.putInt("cav2", cav2Val);
							intent.putExtras(b);
							startActivity(intent);
							finish();
						}
					}catch(Exception ex){
						
					}
					}else if(tv.getId()==cav.getId()&&b.getInt("crop")!=1){
						linearLayout3_1.setVisibility(View.VISIBLE);
						cav2.requestFocus();
					}
				//}catch(Exception ex){
					
				//}
			}
			else{
				onDigit(tv, str, primaryCode);
			}
		}catch(Exception ex){
			
		}
	}
	
	
}